/* Copyright (C) 2024 wlly-lzh
 * Created On 24/03/19
 * Sequence List Stack  基于顺序表的栈
 * */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef struct LSeqStack LSeqStack;
struct LSeqStack {
	void **Data;
	int Length;
	int MaxLength;
};

void Error_LSeqStack_Malloc();
void Error_LSeqStack_NULL();
void Error_LSeqStack_Full();
void Error_LSeqStack_Empty();

LSeqStack *Create_LSeqStack(int length){
	if(length <= 0){
		printf("liblds:LSeqStack: length can not less than 0!\n");
		exit(2);
	}
	LSeqStack *newStack;
	newStack =(LSeqStack *)malloc(sizeof(LSeqStack));
	if(newStack == NULL){
		Error_LSeqStack_Malloc();
		exit(3);
	}
	newStack->Data=(void **)malloc(sizeof(void *)*length);
	if(newStack->Data == NULL){
		Error_LSeqStack_Malloc();
		exit(3);
	}
	newStack->Length=-1;
	newStack->MaxLength=length;
	return newStack;
}
bool Push_LSeqStack(LSeqStack *Stack, void *Data){
	if(Stack == NULL){
		Error_LSeqStack_NULL();
		exit(4);
	}
	if(Data != NULL){
		if(Stack->Length < Stack->MaxLength-1){
			Stack->Length++;
			*((Stack->Data)+Stack->Length)=Data;
			return true;
		}else{
			Error_LSeqStack_Full();
			return false;
		}
	}else{
		Error_LSeqStack_NULL();
		return false;
	}
}
bool Pop_LSeqStack(LSeqStack *Stack, void **Data){
	if(Stack == NULL){
		Error_LSeqStack_NULL();
		exit(4);
	}
	if(Stack->Length > -1){
		*(Data)=*((Stack->Data)+(Stack->Length));
		Stack->Length--;
		return true;
	}else{
		Error_LSeqStack_Empty();
		return false;
	}
}
bool GetTop(LSeqStack *Stack, void **Data){
	if(Stack == NULL){
		Error_LSeqStack_NULL();
		exit(4);
	}
	if(Stack->Length > -1){
		*(Data)=*((Stack->Data)+(Stack->Length));
		return true;
	}else{
		Error_LSeqStack_Empty();
		return false;
	}
}
void Clear_LSeqStack(LSeqStack *Stack){
	if(Stack == NULL){
		Error_LSeqStack_NULL();
		exit(4);
	}
	for(int i=0; i <= Stack->Length; ++i){
		free(*(Stack->Data+i));
	}
	Stack->Length=-1;
}

void Destroy_LSeqStack(LSeqStack **Stack){
	Clear_LSeqStack(*Stack);
	free(*Stack);
	(*Stack)=NULL;
}
bool IsEmpty_LSeqStack(const LSeqStack *Stack){
	if(Stack != NULL ){
		if(Stack->Length == -1)
			return true;
		else
			return false;
	}else{
		Error_LSeqStack_NULL();
		exit(4);
	}
}
bool IsFull_LSeqStack(const LSeqStack *Stack){
	if(Stack != NULL){
		if(Stack->Length == Stack->MaxLength-1)
			return true;
		else
			return false;
	}else{
		Error_LSeqStack_NULL();
		exit(4);
	}
}
int Size_LSeqStack(const LSeqStack *Stack){
	if(Stack != NULL){
		return Stack->Length+1;
	}else{
		Error_LSeqStack_NULL();
		exit(4);
	}
}
int MaxSize_LSeqStack(const LSeqStack *Stack){
	if(Stack != NULL){
		return Stack->MaxLength;
	}else{
		Error_LSeqStack_NULL();
		exit(4);
	}
}

#ifdef _LINUX_PLATFORM_
void Error_LSeqStack_Malloc(){
	printf("\033[31mliblds/LSeqStack: Malloc False !\033[0m\n");
}
void Error_LSeqStack_NULL(){
	printf("\033[31mliblds/LSeqStack: Your parameter Is Null!\033[0m\n");
}
void Error_LSeqStack_Full(){
	printf("\033[31mliblds/LSeqStack: The Stack Is Full!\033[0m\n");
}
void Error_LSeqStack_Empty(){
	printf("\033[31mliblds/LSeqStack: The Stack is Empty!\033[0m\n");
}
#else
void Error_LSeqStack_Malloc(){
	printf("liblds/LSeqStack: Malloc False !\n");
}
void Error_LSeqStack_NULL(){
	printf("liblds/LSeqStack: Your parameter Is Null!\n");
}
void Error_LSeqStack_Full(){
	printf("liblds/LSeqStack: The Stack Is Full!\n");
}
void Error_LSeqStack_Empty(){
	printf("liblds/LSeqStack: The Stack is Empty!\n");
}
#endif
